<?php

namespace App\Library\Service;

use App\Library\Logic\ShopProductLogic;
use App\Library\Logic\SourceMaterialLogic;
use App\Library\Logic\SourceMaterialSubmitRecordLogic;
use App\Library\Requests\Base\SerialNumberRequest;
use App\Library\Requests\Base\SerialNumbersRequest;
use App\Library\Requests\SourceMaterial\SourceMaterialAddRequest;
use App\Library\Requests\SourceMaterial\SourceMaterialPagingRequest;
use App\Library\Requests\SourceMaterial\SourceMaterialSubmitRecordPageRequest;
use Illuminate\Http\JsonResponse;

class SourceMaterialService extends BaseService
{
    public function __construct(SourceMaterialLogic $logic)
    {
        return $this->logic = $logic;
    }

    /**
     *
     * @param SourceMaterialAddRequest $request
     * @return JsonResponse
     *
     */
    public function add(SourceMaterialAddRequest $request): JsonResponse
    {
        $model = $this->logic->insertForModel(array_merge(handleRequest($request), ['userId' => USERID]));
        SourceMaterialSubmitRecordLogic::insertForModel([
            'sourceMaterialId' => $model->id,
            'userId' => USERID,
            'imgAmount' => count($request->imgIds),
            'videoAmount' => count($request->videoIds),
        ]);
        return success();
    }

    /**
     *
     * @param SourceMaterialSubmitRecordPageRequest $request
     * @return JsonResponse
     *
     */
    public function submitRecordPaging(SourceMaterialSubmitRecordPageRequest $request): JsonResponse
    {
        return success(paginate(SourceMaterialSubmitRecordLogic::query()
                ->where('user_id', USERID)
                ->whereBetween('created_at', [
                    now()->parse($request->date)->startOfMonth()->format('y-m-d H:i:s'),
                    now()->parse($request->date)->endOfMonth()->format('y-m-d H:i:s'),
                ])
                ->orderByDesc('created_at')
                ->paginate($request->limit))
        );
    }

    /**
     *
     * @param SerialNumbersRequest $request
     * @return JsonResponse
     *
     */
    public function submitRecordDelete(SerialNumbersRequest $request): JsonResponse
    {
        $model = SourceMaterialSubmitRecordLogic::query()->whereIn('serial_number', $request->serialNumberArr)->pluck('serial_number')->toArray();
        $missing = array_diff($request->serialNumberArr, $model);
        if (!empty($missing)) return failMessage('数据:' . $missing[0] . '不存在');

        SourceMaterialSubmitRecordLogic::query()->whereIn('serial_number', $request->serialNumberArr)->delete();
        return success();
    }

    public function paging(SourceMaterialPagingRequest $request): JsonResponse
    {
        $builder = SourceMaterialLogic::query()
            ->leftJoin('shop_user', 'shop_user.user_id', '=', 'source_material.user_id');

        if (!is_null($request->mobile)) $builder->where('shop_user.user_mobile', $request->mobile);
        if (!is_null($request->title)) $builder->where('shop_user.user_title', 'like', '%' . $request->title . '%');
        return success(paginate(
                $builder->orderByDesc('source_material.created_at')
                    ->paginate($request->limit))
        );
    }

    public function detail(SerialNumberRequest $request): JsonResponse
    {
        $model = SourceMaterialLogic::findBySerialNumber($request);
        if (is_null($model)) return fail(433);
        else return success($model);
    }
}
